package com.og.managedbean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.log4j.Logger;

import com.og.backingbean.Company;
import com.og.backingbean.Contract;
import com.og.dao.CRUDEntityDAO;

/**
 * @author Renaud DIANA
 *
 */
@Named
@SessionScoped
public class ContractBean implements Serializable {

	private static final long serialVersionUID = 1L;

	// Log4j	 
	private static final Logger	log	= Logger.getLogger(ApplicationBean.class);

	/* Dependency injection */
	// Model
    @Inject
    private CRUDEntityDAO<Contract> contractDAO;    
    // BackingBean        
    @Inject
    private Contract contractNew;
    @Inject
    private Contract contractUpdate;
    @Inject
    private Contract contractView;
    @Inject
    private Company company;
    @Inject
    private Company provider;
    @Inject
    private ListBean list;
        
    /* -------------------- */
	    
	/**
	 * Default constructor
	 */
	public ContractBean() {
		// TODO Auto-generated constructor stub
	}

	public String create() {
		
		// Add FK
		List<Company> lc = new ArrayList<Company>();
		lc.add(company);
		lc.add(provider);
		contractNew.setListCompanies(lc);
		
		// Call model		
		contractDAO.createEntity(contractNew);
		
		// Generate new list
		list.findAllContract();
		
		// Clear form
		contractNew = new Contract();
						
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Create contract Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}	
	
	public String update() {
				
		// Call model				
		contractDAO.updateEntity(contractUpdate);
		
		// Toggle view
		contractView = contractUpdate;
				
		// Generate new list
		list.findAllContract();
			
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Update contract Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String delete() {
				
		// Call model				
		contractDAO.deleteEntity(contractView);
		
		// Generate new list
		list.findAllContract();
		
		// Clear form
		contractView = new Contract();
				
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Delete contract Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String findOne() {
		
		// Call model				
		contractView = contractDAO.findOneEntity(contractView, contractView.getIdContract());
					
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Find contract Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}

	public String toggleUpdate() {
		
		contractUpdate = contractView;
		
		return "";
	}
	
	public String viewDetail() {
		
		for(Company c : contractView.getListCompanies()) {
			
			if(c.getIdCompany().substring(3, 4).equals("C"))
				company = c;
			else
				provider = c;
		}
		
		return "";
	}

	// Getters and Setters
	
	public Contract getContractNew() {
		return contractNew;
	}

	public void setContractNew(Contract contractNew) {
		this.contractNew = contractNew;
	}

	public Contract getContractUpdate() {
		return contractUpdate;
	}

	public void setContractUpdate(Contract contractUpdate) {
		this.contractUpdate = contractUpdate;
	}

	public Contract getContractView() {
		return contractView;
	}

	public void setContractView(Contract contractView) {
		this.contractView = contractView;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public Company getProvider() {
		return provider;
	}

	public void setProvider(Company provider) {
		this.provider = provider;
	}
	
}
